/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.elements;

import java.awt.Point;
import java.util.ArrayList;

import plar.core.Action;
import plar.core.Common;
import plar.core.Element;
import plar.core.Parameters;
import plar.core.Sprite;
import plar.core.defaultActions.Move;
import plar.core.defaultElements.Flash;

import java.awt.Point;
import java.awt.Rectangle;


public class Mine extends Flash {
	
	public Mine()
	{
		//setProperty("lifeTime",200);
		//setProperty("explosionRelativeRectangle",new Rectangle(-150,-150,300,300));
		//setProperty("explosionDamage",40);
		actions.addAction("mine",new Action(){
				int timer =0;
				public Parameters call(Parameters parameters)
				{
					Element me = (Element) parameters.get("me");
					String subAction = (String) parameters.get("subAction");
					if (subAction == "main") {
					timer++;
					
					if(timer>=(Integer)me.getProperty("explosionTime")
					&& timer<=(Integer)me.getProperty("lifeTime")) {
						Common.info("mine exploded!");
						Rectangle r = new Rectangle((Rectangle) me.getProperty("explosionRelativeRectangle"));
						Common.info("relative rectnagle"+r);
						r.x += me.getPosition().x;
						r.y += me.getPosition().y;
						ArrayList <Element> al = me.level.getCollisions(me,r);
						for(Element e: al)
						{
							if(e.getProperty("user")!=null)
							{
								Integer energy = (Integer) e.getProperty("energy");
								e.setProperty("energy", energy - ((Integer)me.getProperty("explosionDamage")));
								Common.info("shooted to:" + e);
							}
						}
						//timer=-1;
						
						/*parameters.put("subAction", "new");
						parameters.put("me", me);
						parameters.put("level", level);
						me.actions.runAction("destroy", parameters);
					*/
			}
		}
			return null;
		}
			
		});
	}
}
	
